package com.example.service;

import com.example.dao.ImageDao;
import com.example.entity.ImageEntity;
import com.example.util.IdUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

@Service
public class ImageServiceImpl implements  ImageService {
    @Autowired
    private ImageDao imageDao;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void insertImage(ImageEntity entity) {
        imageDao.insertImage(entity);
    }

    @Override
    public String insertImageByMongo(InputStream inputStream,
                                     String contentType, String fileName) {
        DB db = mongoTemplate.getDb();
        //DBCollection dbcollection = db.getCollection("image");??you什么用
        GridFS gridFS = new GridFS(db,"image");
        String id = IdUtil.getRadomStr();
        GridFSInputFile inputFile = gridFS.createFile(inputStream);
        inputFile.put("_id", id);
        inputFile.put("filename",fileName);
        System.out.println("uploadDate is "+ new Date());
        inputFile.put("uploadDate",new Date());
        inputFile.put("contentType",contentType);
        inputFile.save();
        return id;
    }

    @Override
    public ImageEntity getImageById(int id) {
        return imageDao.getImageById(id);
    }

    @Override
    public ImageEntity getImageMonById(String id) {
        DB db = mongoTemplate.getDb();
        GridFS  gridFS= new GridFS(db,"image");
        //gridFS.findOne(filename);这是通过filename找寻,只有确保文件名不重复才能这样，findOne不知道会不会查多个chunks，试试
        GridFSDBFile file = gridFS.findOne(new BasicDBObject("_id", id));
        ImageEntity entity = new ImageEntity();
        try {
            entity.setData(StreamUtils.copyToByteArray(file.getInputStream()));
            entity.setContentType(file.getContentType());
        } catch (IOException e) {
            System.out.println("读取图片内容失败");
            e.printStackTrace();
        }
        //手动关闭流
        if (null!=file.getInputStream())
        {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return entity;
    }

    @Override
    public void deleteImageMonById(String id) {
        DB db = mongoTemplate.getDb();//这个db用成员变量好不好，不需要，反正也是get方法，获得这个属性的一个东西而已
        GridFS  gridFS= new GridFS(db,"image");
        //gridFS.remove(new ObjectId(id));Objectid会将string误认为i16进制
        gridFS.remove(new BasicDBObject("_id", id));//我他么真是天才
        //删除不存在的也不会报错，不过这个fridFs对象需不需要手动关闭，不然多浪费内存
        //销毁方式可以指NULL 也可以调用 销毁方法,太丑了啊
    }
}
